//
//  NineViewController.m
//  UI_豆瓣
//
//  Created by dllo on 15/11/4.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "NineViewController.h"
#import "NineViewCell.h"
#import "Movie.h"
#import "MBProgressHUD.h"
#import "AppTools.h"
#import "MovieDetailViewController.h"
@interface NineViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic, retain) MBProgressHUD *hub;
@property (nonatomic, retain) NSMutableArray *movieArray;


@end

@implementation NineViewController

- (void)dealloc
{
    [_movieArray release];
    [_hub release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor yellowColor];
    
    //    UICollectionViewFlowLayout 是系统提供的一个标准瀑布流风格
    UICollectionViewFlowLayout * flowLayout = [[UICollectionViewFlowLayout alloc] init];
    
    //    UICollectionView是负责显示和滚动的
    UICollectionView *collectV = [[UICollectionView alloc] initWithFrame:[[UIScreen mainScreen] bounds] collectionViewLayout:flowLayout];
    collectV.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:collectV];
    [collectV release];

    
    //设置每个item(cell)大小
    flowLayout.itemSize = CGSizeMake(110, 150);
    
    //最小行间距
    flowLayout.minimumLineSpacing = 10;
    
    //最小列间距
    flowLayout.minimumInteritemSpacing = 10;
    
    //设置item与四周的边距(上左下右)
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    //设置滚动方向
    flowLayout.scrollDirection =     UICollectionViewScrollDirectionVertical;
    
    //和tableView签代理
    collectV.delegate = self;
    collectV.dataSource = self;
    
    //注册cell
    [collectV registerClass:[NineViewCell class] forCellWithReuseIdentifier:@"cell"];

    
    self.movieArray = [NSMutableArray array];
    
    
    //请求数据
    NSString *urlStr = @"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/movielist.php";
    
    //让菊花旋转起来
    self.hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [self.hub show:YES];
    self.hub.labelText = @"玩命加载ing......";
    [self.view addSubview:self.hub];
    
    [AppTools getDataWithUrlStr:urlStr andBlock:^(id result) {
        
        NSMutableDictionary *dic = result;
        NSString *Code = [dic objectForKey:@"error_code"];
        NSString *reason = [dic objectForKey:@"reason"];
        NSMutableArray *array = [dic objectForKey:@"result"];
        
        //判断是否请求到数据
        if ([reason isEqualToString:@"success"]) {
            
            for (NSDictionary *dic in array) {
                Movie *movie = [[Movie alloc]init];
                [movie setValuesForKeysWithDictionary:dic];
                [self.movieArray addObject:movie];
                [movie release];
                
//                NSLog(@"1111%@",movie);
            }
            
            //让菊花停止
            [self.hub hide:YES];
            
            //刷新页面一定要有数据之后再刷新
            [collectV reloadData];
            
        }
        
    } error:^(NSError *error) {
        
        //让菊花停止
        [self.hub hide:YES];
        
    }];
    

    
    
    
    
    
    
}


#pragma mark --  设置显示item个数

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.movieArray.count;
}

#pragma mark -- 设置cell内容
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentify = @"cell";
    NineViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentify forIndexPath:indexPath];
    cell.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1.0];
  
    Movie *mov = [self.movieArray objectAtIndex:indexPath.item];
    cell.myMovie = mov;
    
    return cell;
}


#pragma mark --  点击item触发方法

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    //    item和row是一样的
//    NSLog(@"item = %ld  section = %ld", indexPath.item, indexPath.section);
    
    
    MovieDetailViewController *detailVC = [[MovieDetailViewController alloc]init];
    
    //属性传值
    Movie *movie = [self.movieArray objectAtIndex:indexPath.item];
    detailVC.movieID = movie.movieId;
    detailVC.mov = movie;
    
    [self.navigationController pushViewController:detailVC animated:YES];
    
    [detailVC release];

    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
